package com.qkst.website.web.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qkst.website.web.bean.OrderBean;
import com.qkst.website.web.mapper.OrderItemMapper;
import com.qkst.website.web.mapper.OrderMapper;
import com.qkst.website.web.service.OrderService;

/**
 * @author lihao
 * @description 订单service
 * @date 2017-05-03
 * @email ahlxlihao@163.com
 *
 */
@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;
	/**
	 * 根据订单条件查询订单
	 * @param order
	 * @return List<Order>
	 */
	public List<OrderBean> queryOrderList(OrderBean order){
		//分页计算
		return orderMapper.selectOrderList(order);
	}
	
	/**
	 * 查询订单总记录数
	 * @param order
	 * @return 
	 */
	@Override
	public int queryCount(OrderBean order) {
		return orderMapper.selectCount(order);
	}

	
	/**
	 * 创建订单
	 * @param order
	 * @return boolean
	 */
	@Override
	public void save(OrderBean order,Map<String, Object> result) {
		if(order.getOrderItemList()!=null && order.getOrderItemList().size()>0){
			order.setCreateTime(new Date());
			order.setLastUpdate(new Date());
			int num=orderMapper.insert(order);//插入订单头
			result.put("order",order);
			if(num>0){//插入订单头成功
				//插入订单项
				if(order.getOrderItemList()!=null && !order.getOrderItemList().isEmpty()){
					orderItemMapper.batchInsert(order);
				}
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//插入失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "444");
		}
	}
	
	/**
	 * 取消订单
	 * @param order
	 * @return 
	 */
	@Override
	public void cancelOrder(OrderBean order, Map<String, Object> result) {
		if(order.getOrderId()!=null){
			order.setStatus("X");//设置订单状态为取消
			order.setLastUpdate(new Date());
			int num=orderMapper.updateByPrimaryKeySelective(order);
			result.put("order",order);
			if(num>0){//取消成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//取消失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "404");
		}
		
	}
	
}
